# -*- coding: utf-8 -*-
import scrapy
import base64
from io import BytesIO
from fontTools.ttLib import TTFont
import requests
import re


url = 'https://xa.58.com/chuzu/?PGTID=0d100000-001e-3b1e-9a0a-2fa0a941f4e5&ClickID=3'
res = requests.get(url)
bs64_str = re.findall("charset=utf-8;base64,(.*?)'\)", res.text)[0]


def get_page_show_ret(string):
    font = TTFont(BytesIO(base64.decodebytes(bs64_str.encode())))
    c = font['cmap'].tables[0].ttFont.tables['cmap'].tables[0].cmap
    ret_list = []
    for char in string:
        decode_num = ord(char)
        if decode_num in c:
            num = c[decode_num]
            num = int(num[-2:]) - 1
            ret_list.append(num)
        else:
            ret_list.append(char)
    ret_str_show = ''
    for num in ret_list:
        ret_str_show += str(num)
    return ret_str_show



class ZufangSpider(scrapy.Spider):
    name = 'zufang'
    allowed_domains = ['xa.58.com','jxjump.58.com']
    start_urls = ['https://xa.58.com/chuzu/?PGTID=0d3090a7-001e-3171-815f-f838f21c9568&ClickID=1']

    def parse(self, response):
        divTags = response.xpath("//div[@class='des']")
        for div in divTags:
            house_url = div.xpath('./h2/a/@href').get()
            name = div.xpath('./h2/a/text()').get().strip()
            name = get_page_show_ret(name)
            yield scrapy.Request(url=house_url,callback=self.parse_detail,meta={'info':(house_url,name)})
    def parse_detail(self,response):
        url,name = response.meta['info']
        infodivs = response.xpath("//div[@class='house-basic-desc']")
        for div in infodivs:
            price = div.xpath(".//b/text()").get()
            price = get_page_show_ret(price)
            address = div.xpath(".//ul/li/span[@class='dz']/text()").get().strip()
            address = get_page_show_ret(address)
            print(price,address)
